package com.wondersgroup.core.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.wondersgroup.core.model.DataDictDetail;

/**
 * 提供字典数据读取使用的一些静态变量和方法
 * @author liufen
 */
public class DataDicConstant {

	/**
     * DATA_DICT_DETAIL
     * TYPE:预约类型(1)
     */
    public static final int APPOINTMENT_TYPE_OUTPATIENT = 1001; //门诊
    public static final int APPOINTMENT_TYPE_EMERGENCY = 1002; //急诊
    public static final int APPOINTMENT_TYPE_HOSPITALIIZATION = 1003; //住院
    public static final int APPOINTMENT_TYPE_PHYSICAL = 1004; //体检

    /**
     * DATA_DICT_DETAIL
     * TYPE:预约状态(2)
     */
    public static final int APPOINTMENT_STATUS_TO_ACCEPT = 2001; //待受理
    public static final int APPOINTMENT_STATUS_ACCEPTED = 2002; //已受理
    public static final int APPOINTMENT_STATUS_CANCELED = 2003; //已撤销
    public static final int APPOINTMENT_STATUS_SUCCESS = 2004; //预约成功
    public static final int APPOINTMENT_STATUS_CLOSE = 2005; //不受理
    
    /**
     * DATA_DICT_DETAIL
     * TYPE:预授权状态(25)
     */
    public static final int AUDIT_STATUS_TO_ACCEPT = 2501; //待受理
    public static final int AUDIT_STATUS_ACCEPTED = 2502; //已受理
    public static final int AUDIT_STATUS_CLOSE = 2503; //不受理
    public static final int AUDIT_STATUS_UNPASS = 2504; //审核未通过
    public static final int AUDIT_STATUS_PASS = 2505; //审核通过
    public static final int AUDIT_STATUS_USED = 2506; //已使用
    
    /**
     * DATA_DICT
     */
    public static final int DIC_APPOINTMENT_TYPE = 1;         //预约类型
    public static final int DIC_APPOINTMENT_STATUS = 2;         //预约状态
    public static final int DIC_COMPANY_STATUS = 3;       //单位状态
    public static final int DIC_COMPANY_TYPE = 4;   //公司类型
    public static final int DIC_CLAIMS_PAYMENT_TYPE = 5;     //理赔给付方式
    public static final int DIC_HOSPITAL_TYPE = 6;   //医院类别
    public static final int DIC_SPECIALTY_TYPE = 7;             //专科类别
    public static final int DIC_ECONOMIC_TYPE = 8;              //经济类型
    public static final int DIC_HOSPITAL_LEVEL = 9;        //医院级别
    public static final int DIC_HOSPITAL_STATUS = 10;   //医院状态
    public static final int DIC_CERTIFICATE_TYPE = 11;       //证件类型
    public static final int DIC_LIVING_STATUS = 12;             // 生存状态
    public static final int DIC_JOB_IDENTIFICATION = 13;           //在职标识
    public static final int DIC_INSURANCE_STATUS = 14;        // 保单状态
    public static final int DIC_INSURANCE_PERIOD_TYPE = 15;        //保险期类型
    public static final int DIC_PAYMENT_METHOD = 16;       //缴费方式
    public static final int DIC_PERIODIC_SETTLEMENT = 17;         // 定期结算
    public static final int DIC_SUBSINGLE_STATUS = 18;      //分单状态
    public static final int DIC_INSURED_RELATIONSHIP = 19;       //被保人关系
    public static final int DIC_LOCK_IDENTITY = 20;   //锁单标识
    public static final int DIC_INSURANCE_TYPE = 21;   //险种类型
    public static final int DIC_LIABILITY_TYPE = 22;       //责任类型 
    public static final int DIC_SUB_CASE_TYPE = 23;   //子陪案件类型 Sub accompany case type
    public static final int DIC_SUB_EVENT_TYPE = 24;   //子陪案事件类型    
    public static final int DIC_AUDIT_STATUS = 25;   //预授权状态

    private static Map<Integer, String> dataDicNameMap;
    private static Map<String, Map<Integer, String>> dataDicDetailMap; //所有该类型下的detail
    private static Map<String, Map<Integer, String>> dataDicRootDetailMap; //不含有父节点的detail
    private static Map<Integer, String> dataDicDetailNameMap;
    private static Map<Integer, Map<Integer, String>> dataDicParentDetailMap;
    private static Map<Integer, DataDictDetail> detailMap;

    /**
     * 根据字典类别ID获取类别名称
     *
     * @param dataDicId
     * @return
     */
    public static String getDataDicNameById(Integer dataDicId) {
        if (dataDicId!=null && dataDicNameMap != null && dataDicNameMap.containsKey(dataDicId)) {
            return dataDicNameMap.get(dataDicId);
        }
        return null;
    }

    /**
     * 根据字典小类别ID获取小类别名称
     *
     * @param dataDicDetailId
     * @return
     */
    public static String getDataDicDetailNameById(Integer dataDicDetailId) {
        if (dataDicDetailId!=null && dataDicDetailNameMap != null && dataDicDetailNameMap.containsKey(dataDicDetailId)) {
            return dataDicDetailNameMap.get(dataDicDetailId);
        }
        return null;
    }

    /**
     * 根据字典类别获取字典集合<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     * 字典类别编号的定义可以参看 DataDicConstant类。
     *
     * @param dataDicType
     * @return
     */
    public static Map<Integer, String> getDataDicDetailByType(Integer dataDicType, Integer comId) {
        String key = dataDicType + ":" + (comId == null?"":comId+"");
    	if (dataDicType!=null && dataDicDetailMap != null && dataDicDetailMap.containsKey(key)) {
            return dataDicDetailMap.get(key);
        }
        return new LinkedHashMap<Integer, String>();
    }

    /**
     * 根据字典类别获取字典集合, 并进行名称过滤<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     * 字典类别编号的定义可以参看 DataDicConstant类。
     *
     * @param dataDicType
     * @return
     */
    public static Map<Integer, String> getDataDicDetailByType(Integer dataDicType, Integer comId, String filterName) {
    	String key = dataDicType + ":" + (comId == null?"":comId+"");
    	if (dataDicType!=null && dataDicDetailMap != null && dataDicDetailMap.containsKey(key)) {
            Map<Integer, String> detailMap =  dataDicDetailMap.get(key);
            if (detailMap != null && detailMap.size() > 0) {
                Map<Integer, String> map = new LinkedHashMap<Integer, String>();
                for (Entry<Integer, String> entry : detailMap.entrySet()) {
                    boolean match = false;
                    Integer id = entry.getKey();
                    String name = entry.getValue();

                    if (filterName == null) {
                        match = true;
                    } else if (name != null && name.contains(filterName)) {
                        match = true;
                    }

                    if (match) {
                        map.put(id, name);
                    }
                }
                return map;
            }
        }
        return new LinkedHashMap<Integer, String>();
    }

    /**
     * 根据字典类别获取字典集合<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的第一层字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     * 字典类别编号的定义可以参看 DataDicConstant类。
     *
     * @param dataDicType
     * @return
     */
    public static Map<Integer, String> getDataDicRootDetailByType(Integer dataDicType, Integer comId) {
    	String key = dataDicType + ":" + (comId == null?"":comId+"");
    	if (dataDicType!=null && dataDicRootDetailMap != null && dataDicRootDetailMap.containsKey(key)) {
            return dataDicRootDetailMap.get(key);
        }
        return new LinkedHashMap<Integer, String>();
    }

    /**
     * 根据字典类别获取字典集合, 并进行名称过滤<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的第一层字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     * 字典类别编号的定义可以参看 DataDicConstant类。
     *
     * @param dataDicType
     * @return
     */
    public static Map<Integer, String> getDataDicRootDetailByType(Integer dataDicType, Integer comId, String filterName) {
    	String key = dataDicType + ":" + (comId == null?"":comId+"");
    	if (dataDicType!=null && dataDicRootDetailMap != null 
        		&& dataDicRootDetailMap.containsKey(key)) {
            Map<Integer, String> detailMap =  dataDicRootDetailMap.get(key);
            if (detailMap != null && detailMap.size() > 0) {
                Map<Integer, String> map = new LinkedHashMap<Integer, String>();
                for (Entry<Integer, String> entry : detailMap.entrySet()) {
                    boolean match = false;
                    Integer id = entry.getKey();
                    String name = entry.getValue();

                    if (filterName == null) {
                        match = true;
                    } else if (name != null && name.contains(filterName)) {
                        match = true;
                    }

                    if (match) {
                        map.put(id, name);
                    }
                }
                return map;
            }
        }
        return new LinkedHashMap<Integer, String>();
    }

    /**
     * 根据字典数据编号获取其下的子数据集合<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有下级字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     *
     * @param parentId
     * @return
     */
    public static Map<Integer, String> getDataDicDetailByParentId(Integer parentId) {
        if (parentId!=null && dataDicParentDetailMap != null && dataDicParentDetailMap.containsKey(parentId)) {
            return dataDicParentDetailMap.get(parentId);
        }
        return new LinkedHashMap<Integer, String>();
    }

    /**
     * 根据字典数据编号获取其下的子数据集合, 并进行名称过滤<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有下级字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     *
     * @param parentId
     * @return
     */
    public static Map<Integer, String> getDataDicDetailByParentId(Integer parentId, String filterName) {
        if (parentId!=null && dataDicParentDetailMap != null && dataDicParentDetailMap.containsKey(parentId)) {
            Map<Integer, String> detailMap =  dataDicParentDetailMap.get(parentId);
            if (detailMap != null && detailMap.size() > 0) {
                Map<Integer, String> map = new LinkedHashMap<Integer, String>();
                for (Entry<Integer, String> entry : detailMap.entrySet()) {
                    boolean match = false;
                    Integer id = entry.getKey();
                    String name = entry.getValue();

                    if (filterName == null) {
                        match = true;
                    } else if (name != null && name.contains(filterName)) {
                        match = true;
                    }

                    if (match) {
                        map.put(id, name);
                    }
                }
                return map;
            }
        }
        return new LinkedHashMap<Integer, String>();
    }

    /**
     * 根据字典数据编号获取其下的子数据集合, 此方法会返回整个树形结构<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有下级字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     *
     * @param parentId
     * @return
     */
    public static Map<Integer, Map<String, Object>> getDataDicDetailTreeByParentId(Integer parentId) {
        return getDataDicDetailTreeByParentId(parentId, null);
    }

    /**
     * 根据字典数据编号获取其下的子数据集合, 并进行名称过滤, 此方法会返回整个树形结构<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有下级字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     *
     * @param parentId
     * @return
     */
    public static Map<Integer, Map<String, Object>> getDataDicDetailTreeByParentId(Integer parentId, String filterName) {
        if (parentId!=null && dataDicParentDetailMap != null && dataDicParentDetailMap.containsKey(parentId)) {
            Map<Integer, String> detailMap = dataDicParentDetailMap.get(parentId);
            if (detailMap != null && detailMap.size() > 0) {
                Map<Integer, Map<String, Object>> map = new LinkedHashMap<Integer, Map<String, Object>>();
                for (Entry<Integer, String> entry : detailMap.entrySet()) {
                    boolean match = false;
                    Integer id = entry.getKey();
                    String name = entry.getValue();

                    if(filterName==null){
                        match = true;
                    }else if(name!=null && name.contains(filterName)){
                        match = true;
                    }

                    Map<Integer, Map<String, Object>> childs = getDataDicDetailTreeByParentId(id, filterName);
                    if (childs == null) {
                        childs = new LinkedHashMap<Integer, Map<String, Object>>();
                    }

                    if(!match && !childs.isEmpty()){
                        match = true;
                    }

                    if (match) {
                        Map<String, Object> valueMap = new LinkedHashMap<String, Object>();
                        valueMap.put("name", name);
                        valueMap.put("childs", childs);
                        map.put(id, valueMap);
                    }
                }
                return map;
            }
        }
        return new LinkedHashMap<Integer, Map<String, Object>>();
    }

    /**
     * 根据字典类别获取字典集合, 此方法会返回整个树形结构<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     * 字典类别编号的定义可以参看 DataDicConstant类。
     *
     * @param dataDicType
     * @return
     */
    public static Map<Integer, Map<String, Object>> getDataDicDetailTreeByType(Integer dataDicType, Integer comId) {
        return getDataDicDetailTreeByType(dataDicType, comId, null);
    }

    /**
     * 根据字典类别获取字典集合, 并进行名称过滤, 此方法会返回整个树形结构<br>
     * 主要用于获取下拉列表，返回字典集合中的某一类别对应的所有字典数据。<br>
     * 例如：<112, 淮扬菜>；<124, 苏帮菜><br>
     * 字典类别编号的定义可以参看 DataDicConstant类。
     *
     * @param dataDicType
     * @return
     */
    public static Map<Integer, Map<String, Object>> getDataDicDetailTreeByType(Integer dataDicType, Integer comId, String filterName) {
    	String key = dataDicType + ":" + (comId == null?"":comId+"");
    	if (dataDicType!=null && dataDicRootDetailMap != null && dataDicRootDetailMap.containsKey(key)) {
            Map<Integer, String> detailMap = dataDicRootDetailMap.get(key);
            if (detailMap != null && detailMap.size() > 0) {
                Map<Integer, Map<String, Object>> map = new LinkedHashMap<Integer, Map<String, Object>>();
                for (Entry<Integer, String> entry : detailMap.entrySet()) {
                    boolean match = false;
                    Integer id = entry.getKey();
                    String name = entry.getValue();

                    if(filterName==null){
                        match = true;
                    }else if(name!=null && name.contains(filterName)){
                        match = true;
                    }

                    Map<Integer, Map<String, Object>> childs = getDataDicDetailTreeByParentId(id, filterName);
                    if (childs == null) {
                        childs = new LinkedHashMap<Integer, Map<String, Object>>();
                    }

                    if(!match && !childs.isEmpty()){
                        match = true;
                    }

                    if (match) {
                        Map<String, Object> valueMap = new LinkedHashMap<String, Object>();
                        valueMap.put("name", name);
                        valueMap.put("childs", childs);
                        map.put(id, valueMap);
                    }
                }
                return map;
            }
        }
        return new LinkedHashMap<Integer, Map<String, Object>>();
    }

    public static Map<Integer, String> getDataDicNameMap() {
        return dataDicNameMap;
    }

    public static void setDataDicNameMap(Map<Integer, String> dataDicNameMap) {
        DataDicConstant.dataDicNameMap = dataDicNameMap;
    }

    public static Map<String, Map<Integer, String>> getDataDicDetailMap() {
        return dataDicDetailMap;
    }

    public static void setDataDicDetailMap(
            Map<String, Map<Integer, String>> dataDicDetailMap) {
        DataDicConstant.dataDicDetailMap = dataDicDetailMap;
    }

    public static Map<Integer, String> getDataDicDetailNameMap() {
        return dataDicDetailNameMap;
    }

    public static void setDataDicDetailNameMap(
            Map<Integer, String> dataDicDetailNameMap) {
        DataDicConstant.dataDicDetailNameMap = dataDicDetailNameMap;
    }

    public static Map<Integer, Map<Integer, String>> getDataDicParentDetailMap() {
        return dataDicParentDetailMap;
    }

    public static void setDataDicParentDetailMap(
            Map<Integer, Map<Integer, String>> dataDicParentDetailMap) {
        DataDicConstant.dataDicParentDetailMap = dataDicParentDetailMap;
    }

    public static Map<String, Map<Integer, String>> getDataDicRootDetailMap() {
        return dataDicRootDetailMap;
    }

    public static void setDataDicRootDetailMap(
            Map<String, Map<Integer, String>> dataDicRootDetailMap) {
        DataDicConstant.dataDicRootDetailMap = dataDicRootDetailMap;
    }
	
	public static Map<Integer, DataDictDetail> getDetailMap() {
		return detailMap;
	}

	public static void setDetailMap(Map<Integer, DataDictDetail> detailMap) {
		DataDicConstant.detailMap = detailMap;
	}
    
    public static Map<String, Integer> getDataDicDetailName2IdMap(int dictType){
        Map<Integer, String> map = DataDicConstant.getDataDicDetailByType(dictType, null);
        Map<String, Integer> nameMap = new HashMap<>();
        if(map!=null && !map.isEmpty()){
            Set<Map.Entry<Integer, String>> entrys = map.entrySet();
            for(Map.Entry<Integer, String> entry : entrys){
                nameMap.put(entry.getValue(), entry.getKey());
            }
        }
        return nameMap;
    }
}
